package com.elitel.frame.base.controller;

import com.elitel.frame.base.service.SysFunctionService;
import com.elitel.frame.main.entity.SysFunction;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.SysFunctionExt;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 项目函数配置
 */
@Controller
@RequestMapping("/system/sysfunc")
public class SysFunctionController {

    private static final Logger logger = LoggerFactory.getLogger(SysFunctionController.class);

    @Autowired
    private SysFunctionService sysFunctionService;

    /**
     * 分页查询后函数脚本信息
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @param seachTxt 模糊匹配内容
     * @param pcode    项目编码
     * @return
     */
    @RequestMapping("/getSysFuncByPagination")
    @ResponseBody
    public Object getSysFuncByPagination(String pageNo, String pageSize, String seachTxt,String pcode){
        JsonResult jr = new JsonResult();
        try {
            PageRequestVo<SysPrjservice> pr = new PageRequestVo<SysPrjservice>();
            pr.setSearchContent(seachTxt);
            pr.setPageNo(Integer.valueOf(pageNo));
            pr.setPageSize(Integer.valueOf(pageSize));
            pr.setTreeCode(pcode);
            PageResponseVo<SysFunctionExt> pagelist = sysFunctionService.getSysFuncByPagination(pr);
            jr.setData(pagelist);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("项目函数信息加载异常！");
            logger.error(e.getMessage(),e);
        }
        return jr;
    }

    /**
     * 函数添加服务
     * @param sysFunction
     * @return
     */
    @RequestMapping("/insertSysFunction")
    @ResponseBody
    public Object insertSysFunction(@RequestBody SysFunction sysFunction){
        JsonResult jr = new JsonResult();
        try {
            sysFunctionService.insertSysFunction(sysFunction);
            jr.setSuccess(true);
            jr.setMsg("函数脚本添加成功");
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("函数脚本添加异常！");
            logger.error(e.getMessage(),e);
        }

        return jr;
    }

    /**
     * 根据主键获取函数脚本
     * @param guid
     * @return
     */
    @RequestMapping("/getSysFunctionByGuid")
    @ResponseBody
    public Object getSysFunctionByGuid(String guid){
        JsonResult jr = new JsonResult();
        try {
            SysFunction sysFunction = sysFunctionService.getSysFunctionByGuid(guid);
            jr.setSuccess(true);
            jr.setData(sysFunction);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("获取函数脚本异常！");
            logger.error(e.getMessage(),e);
        }
        return jr;
    }

    /**
     * 函数修改接口
     * @param sysFunction
     * @return
     */
    @RequestMapping("/updateSysFunction")
    @ResponseBody
    public Object updateSysFunction(@RequestBody SysFunction sysFunction){
        JsonResult jr = new JsonResult();
        try {
            sysFunctionService.updateSysFunction(sysFunction);
            jr.setSuccess(true);
            jr.setMsg("函数脚本修改成功");
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("函数脚本修改异常！");
            logger.error(e.getMessage(),e);
        }
        return jr;
    }

    /**
     * 根据主键批量删除函数脚本信息
     * @param guids
     * @return
     */
    @RequestMapping("/deleteSysFunctionByGuids")
    @ResponseBody
    public Object deleteSysFunctionByGuids(String guids){
        JsonResult jr = new JsonResult();
        try {
            Integer size = sysFunctionService.deleteSysFunctionByGuids(guids);
            jr.setSuccess(true);
            jr.setMsg("函数脚本删除成功！");
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("函数脚本删除异常！");
            logger.error(e.getMessage(),e);
        }
        return jr;
    }

    /**
     * 根据项目编码获取函数脚本信息
     * @param pcode
     * @return
     */
    @RequestMapping("/getSysFunctionByPcode")
    @ResponseBody
    public Object getSysFunctionByPcode(String pcode){
        List<SysFunctionExt> list = sysFunctionService.getSysFunctionByPcode(pcode);
        return list;
     }
}
